package mes.kanban.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import mes.kanban.entity.MesPermission;
import mes.kanban.entity.MesRole;
import mes.kanban.entity.MesUser;
import mes.kanban.respository.UserMessageRespository;
import mes.kanban.respository.UserPermissionRespository;
import mes.kanban.respository.UserRoleRespository;
import mes.kanban.service.UserMessageServiceI;
import mes.kanban.util.IsNotNullValit;
import mes.kanban.util.PageBean;
import mes.kanban.vo.MesUserVO;

@Service
public class UserMessageServiceImpl implements UserMessageServiceI {
	
	private String algorithmName = "md5";
	
    private int hashIterations = 2;
	
	@Autowired
	private UserMessageRespository userMessageRespository;
	
	@Autowired
	private UserRoleRespository userRoleRespository;
	
	@Autowired
	private UserPermissionRespository userPermissionRespository;

	@Override
	public MesUser findUserById(String id) {
		
		return userMessageRespository.findOne(id);
	}

	@Override
	public List<MesUser> findUsers() {
		return userMessageRespository.findAll();
	}

	@Override
	public void saveUser(MesUser user) {
		
		List<MesUser> users = this.userMessageRespository.findUsersByCode(user.getUserCode());
		
		if (IsNotNullValit.CollectionIsNotNull(users)) {
			throw new RuntimeException("已存在账号");
		}else {
			user.setPassWord(encryptPassword(user.getPassWord(), user.getUserCode()));
			userMessageRespository.save(user);
		}
	}
	
	private String encryptPassword(String password,String salt) {
		
        return new SimpleHash(algorithmName, password,  ByteSource.Util.bytes(salt), hashIterations).toHex();

    }

	@Override
	public void deleteUserById(String id) {
		//删除用户
		userMessageRespository.delete(id);
		
		//删除用户对应的角色信息
		userMessageRespository.deleteRoleUser(id);
		
	}

	@Override
	public void updateUser(MesUser user) {
		userMessageRespository.saveAndFlush(user);
	}

	@Override
	public void updateUserByNotNUll(MesUser user) {
		
		MesUser u = this.findUserById(user.getId());
		
		if (!IsNotNullValit.StringIsNotNull(user.getEmail())) {
			user.setEmail(u.getEmail());
		}
		
//		if (!IsNotNullValit.StringIsNotNull(user.getJobLevel())) {
//			user.setJobLevel(u.getJobLevel());		
//		}
		
		if (!IsNotNullValit.StringIsNotNull(user.getPassWord())) {
			user.setPassWord(u.getPassWord());
		}
		
		if (!IsNotNullValit.StringIsNotNull(user.getUserName())) {
			user.setUserName(u.getUserName());
		}
		
		
		userMessageRespository.saveAndFlush(user);
	}
	
	@Override
	public List<MesUser> findUsersByOther(String userCode, String password) {
		return userMessageRespository.findUsersByOther(userCode,password);
	}
	
	@Override
	public MesUser findUsersByCode(String userCode) {

		List<MesUser> users = userMessageRespository.findUsersByCode(userCode);
		
		if (IsNotNullValit.CollectionIsNotNull(users)) {
			return users.get(0);
		}
		
		return null;
	}
	

	@Override
	public MesUserVO findUsersByCodeForShiro(String userCode) {
		
		List<MesUser> users = userMessageRespository.findUsersByCode(userCode);
		
		if (IsNotNullValit.CollectionIsNotNull(users)) {
			
			MesUserVO userVO = new MesUserVO();
			
			BeanUtils.copyProperties(users.get(0), userVO);
			
			//把用户对应的角色取出来
			List<MesRole> roles = userRoleRespository.findRoleByUserCode(userCode);
			
			userVO.setRoles(roles);
			
			//把用户对应的权限取出来
			if (IsNotNullValit.CollectionIsNotNull(roles)) {
				
				List<String> role_ids = new ArrayList<String>();
				
				for (MesRole mesRole : roles) {
					role_ids.add(mesRole.getId());
				}
				
				List<MesPermission> permissions = userPermissionRespository.findPermissionByRoleId(role_ids);
				
				userVO.setPermissions(permissions);
			}
			
			return userVO;
			
		}
		
		return null;
	}
	
	@Override
	public PageBean findUserForPageBean(Integer pageNum, Integer pageSize) {
		
		PageBean pb = new PageBean();
		
		Pageable pageable = new PageRequest(pageNum-1, pageSize);
		
		Page<MesUser> ps = this.userMessageRespository.findAll(pageable);
		
		List<MesUser> us = ps.getContent();
		
		int allRow = (int) ps.getTotalElements();
		
		int totalPage = ps.getTotalPages();
		
		pb.setAllCounts(allRow);
		pb.setPageSize(pageSize);
		pb.setCurrentPage(pageNum);
		pb.setTotalPage(totalPage);
		pb.setList(us);
		
		
		return pb;
	}

}
